<?php
 class Core_Business_Category extends Core_Business_Abstract {
	private $_id;
	private $_name;
	private $_parent;
	
	public function getId() {return $this->_id;}
	
	public function setId($value) {
		if($value) {
			$this->_id = $value;
		}
		else { 
			throw new Exception("Property value for object ".__CLASS__.", property ".__METHOD__);
		}	
	}
	public function getName() {return $this->_name;}
	public function setName($value) {
		if($value) {
			$this->_name = $value;
		}
		else {
			throw new Exception("Property value for object ".__CLASS__.", property ".__METHOD__);	
		}
	}
	/***
	 * getParent - property returning the parent category business class for the current object
	 * @return Core_Business_Category|null
	 */
	public function getParent() {
		if(isset($this->_parent)) {
			$mapper = new Core_Data_Mappers_Category();
			$object = $mapper->find($this->_parent);
			return $object;
		}
		else
			return $this->_parent;
	}
	
	/***
	 * setParent - this property return the parent category.
	 * @param $object Core_Business_Category instance of parent object
	 */
	public function setParent($object) {
		if ($object instanceof Core_Business_Category){
			$this->_parent = $object->getId();
		}
	}
	
	public static function getAll() {
		$map = new Core_Data_Mappers_Category();
		$collection = $map->fetchAll(array("parentid is null"));
		return $collection;
	}
	public static function getCategoryById($id) {
		$map = new Core_Data_Mappers_Category();
		$category = $map->find($id);
		return $category;
	}
	public static function getCategoryByParent($id) {
		$map = new Core_Data_Mappers_Category();
		$categories = $map->fetchAll(array('parentid=?'=>$id),null,null);
		return $categories;
	}
	
	public function __construct() {
		$this->_parent = null;
	}
	
}
?>
